
//
//  FLLActivityViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/8.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLActivityViewController.h"
#import "FLLMacro.h"
#import "FLLActivityTableViewCell.h"
#import "ZWYNetTool.h"
#import "FLLActivityModel.h"
#import "ActivityData.h"
#import "ActivitySecData.h"
#import "FLLMoreTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface FLLActivityViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *activityTableView;
@property (nonatomic, strong) FLLActivityModel *fllActivityModel;
@property (nonatomic, strong) NSMutableArray<ActivitySecData *> *mutArray;
@property (nonatomic, strong) NSArray<ActivitySecData *> *updateArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger currentActivityCount;
@end

@implementation FLLActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self createPOSTRequest];
    [self createActivityTableView];
}
- (void)viewWillAppear:(BOOL)animated
{

    self.tabBarController.tabBar.hidden = YES;
}
- (void)createPOSTRequest
{
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:@"methodName=ShequActivity&version=4.3&page=1&size=10&user_id=0&_time=&_signature=&" andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.currentPage = 1;
        self.fllActivityModel = [FLLActivityModel modelWithDic:result];
        self.mutArray = (NSMutableArray *)self.fllActivityModel.data.data;
        [self.activityTableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
}
- (void)createActivityTableView
{

    self.activityTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:_activityTableView];
    _activityTableView.delegate = self;
    _activityTableView.dataSource = self;
    _activityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_activityTableView registerNib:[UINib nibWithNibName:@"FLLActivityTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"activityTableCell"];
    [_activityTableView registerNib:[UINib nibWithNibName:@"FLLMoreTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"moreTableCell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{


    return _mutArray.count + 1;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    if (indexPath.row == _mutArray.count) {
        FLLMoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreTableCell"];
        NSInteger totalCount = [self.fllActivityModel.data.total integerValue];
        self.currentActivityCount = totalCount  - _currentPage * 10;
        cell.grayLabel.text = [NSString stringWithFormat:@"还剩下%ld个活动", _currentActivityCount ];
        
        cell.grayLabel.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1.00];
        cell.contentView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
        return cell;
    }
    FLLActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activityTableCell"];
    cell.grayLabel.backgroundColor = FLLLightGrayColor;
    [cell.activityImageView sd_setImageWithURL:[NSURL URLWithString:_mutArray[indexPath.row].image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
    }];
    cell.userCountLabel.text = [NSString stringWithFormat:@"%ld人参与", _mutArray[indexPath.row].user_count];
    cell.userCountLabel.font = [UIFont boldSystemFontOfSize:14 * kWidthScale];
    cell.endTimeLeftLabel.text = [NSString stringWithFormat:@"结束倒计时: 还剩下%ld天", _mutArray[indexPath.row].end_time_left / 3600 / 24];
    cell.endTimeLeftLabel.font = [UIFont boldSystemFontOfSize:14 * kWidthScale];

    return cell;
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == _mutArray.count) {
        if (_currentActivityCount <= 0) {
            return 0;
        }
        return 60 * kHeightScale;
    }
    
    return 180 * kHeightScale;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSString *bodyString = [NSString stringWithFormat:@"methodName=ShequActivity&version=4.3&page=%ld&size=10&user_id=0&_time=&_signature=&", _currentPage  + 1];
    if (indexPath.row == _mutArray.count) {
        [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
            
            _currentPage++;
            self.fllActivityModel = [FLLActivityModel modelWithDic:result];
            self.updateArray = self.fllActivityModel.data.data;
            [_mutArray addObjectsFromArray:_updateArray];
            [self.activityTableView reloadData];
            
        } andFailureBlock:^(NSError *error) {
            
        }];

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
